<?php

/**
 * myDoctor form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class myDoctorForm extends BaseDoctorForm {

	public function configure() {
		unset(
			$this['created_at'], $this['updated_at']
		);

		// grab the gender generated options
		$genderOptions = $this->widgetSchema['gender']->getOptions("choices");

		$this->widgetSchema['user_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['practice_name'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['practice_number'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['professional_statement'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['speciality_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['first_name'] = new sfWidgetFormInputText(array());
		$this->widgetSchema['last_name'] = new sfWidgetFormInputText(array());
		$this->widgetSchema['gender']->setOption("choices", array_merge(array("" => "Please select a gender"), $genderOptions["choices"]));
		$this->widgetSchema["gender"]->setAttribute("class", "select-box-small");
		$this->widgetSchema['language_id']->setOption("add_empty", "Please select a language");
		$this->widgetSchema["language_id"]->setAttribute("class", "select-box-small");

		$this->validatorSchema['first_name'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *first name* is required.'));
		$this->validatorSchema['last_name'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *last name* is required.'));
		$this->validatorSchema['user_id'] = new sfValidatorString(array('required' => false));
		$this->validatorSchema['gender'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *gender* is required.'));
		$this->validatorSchema['language_id'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *language* is required.'));

		$this->setDefaults(array(
			'user_id' => $this->getObject()->getUserId(),
			'first_name' => $this->getObject()->getUser()->getFirstName(),
			'last_name' => $this->getObject()->getUser()->getLastName()
		));
	}

	/**
	 * Override the save method to save the merged user form.
	 */
	public function save($con = null) {
		$this->updateUser();
		return parent::save($con);
	}

	/**
	 * Updates the user merged form
	 */
	protected function updateUser() {
		if (!is_null($user = $this->getUser())) {
			$user->setFirstName($this->getValue("first_name"));
			$user->setLastName($this->getValue("last_name"));
			$user->save();

			unset($this->values['first_name'], $this->values['last_name']);
		}
	}

	/**
	 * Returns the user object. If it does not exist return a new instance.
	 *
	 * @return sfGuardUser
	 */
	protected function getUser() {
		if (!$this->getObject()->getUser()) {
			return new GfUser();
		}

		return $this->getObject()->getUser();
	}

}
